

namespace AlertMail
{
    using System;
    using System.Data;
    interface IRecruiterAlert
    {
        void SendEmailToRecruiters(string schedule);
    }

    public class RecruiterAlert : IRecruiterAlert
    {
        public RecruiterAlert()
        {
            
        }
          
        public void SendEmailToRecruiters(string schedule)
        {
            try
            {
                SqlServerProvider sqlServer = new
                SqlServerProvider();
                sqlServer.CommandText =
                "select * from tblResumeAlerts" +
                " where Activate=1 and " +
                "Schedule='" + schedule + "'";
                DataTable dataTableAlert =
                sqlServer.GetDataTable();
                string keyWord = "";
                string location = "";
                string category = "0";
                string industry = "0";
                string certificate = "0";
                string emailAddress = "";
                DataTable dataTable;
                if (dataTableAlert.Rows.Count > 0)
                {
                    for (int i = 0; i < dataTableAlert.Rows.Count; i++)
                    {
                        keyWord = dataTableAlert.Rows[i]["KeyWord"].ToString();
                        location = dataTableAlert.Rows[i]["CityName"].ToString();
                        category = dataTableAlert.Rows[i]["JobCategoryID"].ToString();
                        industry = dataTableAlert.Rows[i]["JobIndustryID"].ToString();
                        certificate = dataTableAlert.Rows[i]["CertificateID"].ToString();
                        emailAddress = dataTableAlert.Rows[i]["Email"].ToString();
                        dataTable = GetResumes(keyWord, location, category, industry, certificate);
                        string contentToSend = "";
                        if (dataTable != null)
                            contentToSend = CreateContentForRecruiter(dataTable);
                        if (contentToSend != "")
                            SendMailToRecruiter(emailAddress, keyWord, contentToSend);
                    }
                }
                sqlServer.CloseConnection();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Recruiter::SendEmails", ex);
            }
        }
        private void SendMailToRecruiter(string emailTo, string subject, string contentToSend)
        {
            MailProvider mailProvider = new MailProvider();
            mailProvider.SendMail(emailTo, subject, contentToSend);
        }
        private string CreateContentForRecruiter(DataTable dataTable)
        {
            string siteUrl = "<a href=\"http://www.recruitvietnam.com/" +
            "recruiters/ResumeDetails.aspx?";
            string content = "Dear Recruiter\n";
            content += "RecruitVietnam send " +
            "alert resumes for you to reference. \n";

            try
            {
                content += "<table border=0 width=100%>";
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    content += "<tr><td>";
                    content = siteUrl + "ResumeID="
                    + dataTable.Rows[i]["ResumeID"].ToString()                    
                    + " \">" +
                    dataTable.Rows[i]["FullName"].ToString()
                    + "</a>" + "</td><td>" +
                    dataTable.Rows[i]["JobPosition"].ToString()
                    + "</td><td>" +
                    dataTable.Rows[i]["JobCategoryName"].ToString() + "</td></tr>";
                }
                content += "</table>";
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Recruiter::CreateContentForRecruiter", ex);
            }
            return content;
        }
        private DataTable GetResumes(string keyWord,
        string location, string category,
        string industry, string certificate)
        {
            DataTable dataTable = null;
            try
            {
                SqlServerProvider sqlServerProvider =
                    new SqlServerProvider();

                sqlServerProvider.ParameterCollection
                = new string[5] 
                {"@KeyWord", "@WorkLocation", 
                "@Category", "@Industry", "@Certificate"};
                sqlServerProvider.ValueCollection
                = new string[5] 
                { "%" + keyWord + "%", "%" + 
                location + "%", category, 
                industry, certificate};
                sqlServerProvider.CommandType =
                CommandType.StoredProcedure;

                sqlServerProvider.CommandText =
                "SPGetResumeAlerts";
                dataTable =
                sqlServerProvider.GetDataTable();
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Recruiter::SendEmails", ex);
            }
            return dataTable;
        }
    }
}
